package com.awan.controller;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class UserItem {
	
	public String username;
	public String userId;
	public String fullName;
	public String gender;
	public String onLine;
	public String avatar;
	public String status;
	
	private static String TAG = "UserItem";
	
	public static List<UserItem> fromJsonArrayString(String jsonArrayString) {
		//Log.e(TAG, "jsonArrayString : "+jsonArrayString);
		List<UserItem> result=new ArrayList<UserItem>();
		try {
			JSONObject jsonResponse = new JSONObject(jsonArrayString);
			String status = jsonResponse.getString("STATUS");
			if(status.trim().equalsIgnoreCase("SUCCESS")){
				JSONArray users = new JSONArray(jsonResponse.getString("DATA"));				
				for (int i=0;i<users.length();i++) {
					UserItem ui=fromJson(users.getJSONObject(i));				
					result.add(ui);
				}
			}else{
				Log.e(TAG, "STATUS "+status);
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "JSONException[fromJsonArrayString]:"+e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	public static UserItem fromJson(JSONObject jsonObject) {
		UserItem result=null;
		try {
			//JSONObject data = jsonObject.getJSONObject("DATA");
			//if(data != null){				
				result=new UserItem();
				
				result.username	= jsonObject.getString("username");
				result.userId	= jsonObject.getString("user_id");
				result.fullName	= jsonObject.getString("fullname");
				result.gender	= jsonObject.getString("gender");
				result.onLine	= jsonObject.getString("online");
				result.avatar	= jsonObject.getString("avatar");
				result.status	= jsonObject.getString("status");
				
			//}else{
			//	Log.e(TAG, "Data null");
			//}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "JSONException[fromJson]:"+e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

}
